# encoding: utf-8

module ValidateCaptcha

  module InstanceMethods

    def validate_captcha
      if !self.valid_with_captcha?
        self.errors.add(:captcha_key, "验证码错误")
        return false
      end
    end

    #monkey path
    #override simle_captcha's method which has a bug on valid?
    def valid_with_captcha?
      is_captcha_valid?
    end
  
  end

  def self.included(receiver)
    receiver.class_eval do
      apply_simple_captcha
    end
    receiver.module_eval do
      before_validation :validate_captcha, :if => :need_captcha 
      attr_accessor :need_captcha
    end

    receiver.send :include, InstanceMethods
  end

end
